﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using System.ComponentModel;
using Microsoft.Win32;

namespace GetCarInfo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            string[] Kind = carInfoService.GetAllcartype();
            for (int i = 0; i < Kind.Length; i++)
            {
                this.comBKind.Items.Add(Kind[i]);
            }
        }

        private System.Data.Objects.ObjectQuery<CarInfo> GetCarInfoesQuery(CarInfoEntitiesyin carInfoEntitiesyin)
        {
            // Auto generated code

            System.Data.Objects.ObjectQuery<GetCarInfo.CarInfo> carInfoesQuery = carInfoEntitiesyin.CarInfoes;
            // Returns an ObjectQuery.
            return carInfoesQuery;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            GetCarInfo.CarInfoEntitiesyin carInfoEntitiesyin = new GetCarInfo.CarInfoEntitiesyin();
            // Load data into CarInfoes. You can modify this code as needed.
            System.Windows.Data.CollectionViewSource carInfoesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("carInfoesViewSource")));
            System.Data.Objects.ObjectQuery<GetCarInfo.CarInfo> carInfoesQuery = this.GetCarInfoesQuery(carInfoEntitiesyin);
            carInfoesViewSource.Source = carInfoesQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            decimal strNum = (this.comBPrise.SelectedIndex - 1) * 10;
            decimal endNum = (this.comBPrise.SelectedIndex) * 10;
            if (this.comBPrise.SelectedIndex == 0)
            {
                strNum = 0;
                endNum = 0;
            }
            if (this.comBPrise.SelectedIndex == 11)
            {
                endNum = 0;
            }
            string strKind = this.comBKind.SelectedValue.ToString();
            if (this.comBKind.SelectedIndex == 0)
            {
                strKind = null;
            }
            carInfoesDataGrid.ItemsSource = carInfoService.GetCarList(strKind, strNum, endNum);
        }

        Thread updating ;
        public delegate void NextPrimeDelegate();
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            updating = new Thread(StartUpdate);
            updating.Start();
            (sender as Button).IsEnabled = false;
        }
        
        private void StartUpdate()
        {

            //Thread.Sleep(1000 * 1000);

            //save to db
            try
            {
                var e = CarBLL.GetAllCar();
                carInfoService.Save(e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                btnUpdate.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(ChangeBtnStatus));
            }
            MessageBox.Show("Update completed!");
        }

        private void ChangeBtnStatus()
        {
            btnUpdate.IsEnabled = true;
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            updating.Abort();
            btnUpdate.IsEnabled = true;
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            if (updating != null && updating.IsAlive)
            {
                if (MessageBox.Show("正在更新，是否停止？", "询问", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                    updating.Abort();
                }
            }
        }

    }


    public class RevertConvert : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }
    }


}
